/*
* ============================================================================
*  Name     : CMyPicture from MyPicture.cpp
*  Part of  : RichTextEditor
*  Created  : 05/08/2006 by Forum Nokia 
*  Version  : 2.0
*  Copyright: Forum Nokia
* ============================================================================
*/

// INCLUDE FILES
#include "MyPicture.h"
#include <fbs.h>
#include <gulfont.h> 
#include <eikenv.h>
#include <e32base.h>

#include "MyDataCenter.h"
extern CMyDataCenter* iDataCenter;

// ---------------------------------------------------------
// Constructor
// ---------------------------------------------------------
//
CMyPicture::CMyPicture( TSize aSize, CFbsBitmap& aBitmap, CFbsBitmap& aMaskBitmap )
	: iSizeInTwips(aSize), iBitmap(&aBitmap), iMaskBitmap(&aMaskBitmap)
    {

    }

// ---------------------------------------------------------
// LineBreakPossible()
// ---------------------------------------------------------
//
TBool CMyPicture::LineBreakPossible( TUint /*aClass*/,
										   TBool /*aBeforePicture*/,
										   TBool /*aHaveSpaces*/ ) const
	{
	return EFalse;
	}

// ---------------------------------------------------------
// Draw()
// ---------------------------------------------------------
//
void CMyPicture::Draw( CGraphicsContext& aGc,
						     const TPoint& aTopLeft,
						     const TRect& aClipRect,
						     MGraphicsDeviceMap* aMap ) const
	{
	TRect bitmapRect = aMap->TwipsToPixels(TRect(TPoint(),iSizeInTwips));
	TRect srctect = aMap->TwipsToPixels(TRect(TPoint(),iSizeInTwips));	
	bitmapRect.Move(aTopLeft);
	aGc.Reset();
	aGc.SetClippingRect(aClipRect);
//	aGc.DrawBitmap(bitmapRect, iBitmap);
	aGc.DrawBitmapMasked(bitmapRect, iBitmap, srctect, iMaskBitmap, false);


//test	
aGc.SetPenColor	(TRgb(0,0,255));
//aGc.DrawLine(aTopLeft, TPoint(aTopLeft.iX+100,aTopLeft.iY));
//aGc.DrawLine(bitmapRect.iTl, bitmapRect.iBr);
	}

// ---------------------------------------------------------
// ExternalizeL()
// ---------------------------------------------------------
//
void CMyPicture::ExternalizeL( RWriteStream& /*aStream*/ ) const
	{
    // No implementation required
	}
		
// ---------------------------------------------------------
// SetOriginalSizeInTwips()
// ---------------------------------------------------------
//
void CMyPicture::SetOriginalSizeInTwips( TSize aSize )
	{
	iSizeInTwips = aSize;
	}

// ---------------------------------------------------------
// GetOriginalSizeInTwips()
// ---------------------------------------------------------
//
void CMyPicture::GetOriginalSizeInTwips( TSize& aSize ) const
	{
	aSize = iSizeInTwips;
	}

/*
 * class preview, created by wy, at 2008.10.03
 */

CMyPreview::CMyPreview( TSize aSize, CFbsBitmap& aBitmap, CFbsBitmap& aMaskBitmap )
	:CMyPicture(aSize, aBitmap, aMaskBitmap)
{
	iSelected = false;
	iItemSelected = 0;
	iOwner = NULL;
	iItemsInLine = 4;
	iScrollItems = 0;
} 

void CMyPreview::Draw( CGraphicsContext& aGc,
						     const TPoint& aTopLeft,
						     const TRect& aClipRect,
						     MGraphicsDeviceMap* aMap ) const
{

} 


TKeyResponse CMyPreview::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
{

}

//my memo class, multiline edit
CPicMemo::CPicMemo( TSize aSize, CFbsBitmap& aBitmap )
	: iSizeInTwips(aSize), iBitmap(&aBitmap)
    {
		iSelected = false;
		TBuf<128> buf;
		iText = buf.AllocL();		
    }

CPicMemo::~CPicMemo()
{
	delete iText;
}

TBool CPicMemo::LineBreakPossible( TUint /*aClass*/,
										   TBool /*aBeforePicture*/,
										   TBool /*aHaveSpaces*/ ) const
	{
	return EFalse;
	}

void CPicMemo::Draw( CGraphicsContext& aGc,
						     const TPoint& aTopLeft,
						     const TRect& aClipRect,
						     MGraphicsDeviceMap* aMap ) const
	{
	TRect bitmapRect=aMap->TwipsToPixels(TRect(TPoint(),iSizeInTwips));
	bitmapRect.Move(aTopLeft);
	aGc.Reset();
	aGc.SetClippingRect(aClipRect);
	aGc.DrawBitmap(bitmapRect, iBitmap);

	aGc.SetPenColor	(iSelected?TRgb(255,128,64):TRgb(182,202,226));		
	aGc.DrawRect(bitmapRect);
	
	aGc.SetPenColor(TRgb(9, 35, 121));
	const CFont* font = CEikonEnv::Static()->DenseFont();
	aGc.UseFont( font);
	TRect rc = bitmapRect;
	TInt baseline =	font->AscentInPixels() + 5;
	rc.iTl.iX += 3;

//	aGc.DrawText(iText->Des(), rc, baseline);
	
TBidiText* bidi = TBidiText::NewL(iText->Des(), 5);
bidi->WrapText(rc.Width(), *font, NULL, 5);	
TInt lines = bidi->NumberOfLinesInDisplayText();
if(lines == 0)
	aGc.DrawText(iText->Des(), rc, baseline);	
else
	{
		int addheight = font->HeightInPixels() + 3;
	    for(TInt i=0; i<lines; i++)
        {
	        TInt width = 0;
	        TPtrC text = bidi->LineOfDisplayText(i, width);
	        aGc.DrawText(text, rc, baseline, CGraphicsContext::ELeft, 1);
        	rc.Move(0, addheight);
        }		
}
delete bidi;

	
	aGc.DiscardFont();	

//test	
//aGc.SetPenColor	(TRgb(0,0,255));
//aGc.DrawLine(aTopLeft, TPoint(aTopLeft.iX+100,aTopLeft.iY));
	}

void CPicMemo::ExternalizeL( RWriteStream& /*aStream*/ ) const
	{
    // No implementation required
	}

void CPicMemo::SetOriginalSizeInTwips( TSize aSize )
	{
	iSizeInTwips = aSize;
	}

void CPicMemo::GetOriginalSizeInTwips( TSize& aSize ) const
	{
	aSize = iSizeInTwips;
	}
	
void CPicMemo::SetText(const TDesC& aText)
{
	iText = iText->ReAllocL(iText->Length() + aText.Length());
	iText->Des().Copy(aText);
}	
		 
//EOF
